﻿#if UNITY_EDITOR

using UnityEngine;

namespace AI.Graph.Editor {


    public class NewConnectionEvent : GraphEvent {

        private ConnectionPort m_selectedPort;


        public override void OnProcess (Event e) {
            // select port
            if (e.IsMouseDown (MouseButton.Left) && m_selectedPort == null && e.IsMouseInsideRect (canvas.unscaledRect)) {
                for (int i = 0; i < canvas.nodes.Length; i++) {
                    Node currentNode = canvas.nodes[i];

                    if (currentNode.isHidden) {
                        continue;
                    }

                    ConnectionPort[] currentNodePorts = currentNode.ports;

                    for (int j = 0; j < currentNodePorts.Length; j++) {
                        ConnectionPort currentNodePort = currentNodePorts[j];

                        if (!currentNodePort.isHidden && currentNodePort.rect.Contains (canvas.mousePosition)) {
                            m_selectedPort = currentNodePort;
                            canvas.selectedPort = currentNodePort;

                            break;
                        }
                    }

                    if (m_selectedPort != null) {
                        e.Use ();
                        break;
                    }
                }
            }


            // drag port
            if (e.IsMouseDrag (MouseButton.Left) && m_selectedPort != null) {
                graph.Repaint ();
                e.Use ();
            }

            // connect
            if (e.IsMouseUp (MouseButton.Left) && m_selectedPort != null) {
                bool connectionExecuted = false;

                for (int i = 0; i < canvas.nodes.Length; i++) {
                    Node currentNode = canvas.nodes[i];

                    if (currentNode.isHidden) continue;

                    ConnectionPort[] currentNodePorts = currentNode.ports;

                    for (int j = 0; j < currentNodePorts.Length; j++) {
                        ConnectionPort currentNodePort = currentNodePorts[j];

                        if (!currentNodePort.isHidden && currentNodePort.rect.Contains (canvas.mousePosition)) {
                            ConnectionPort fromPort = m_selectedPort;
                            ConnectionPort toPort = currentNodePort;

                            bool connectionSuccess = Serializer.ConnectPorts (fromPort, toPort);

                            if (connectionSuccess) {
                                GraphUndo.Record (fromPort.node.name + " connected to " + toPort.node.name,
                                    () => { Serializer.DisconnectPorts (fromPort, toPort); },
                                    () => { Serializer.ConnectPorts (fromPort, toPort); });
                            }

                            connectionExecuted = true;
                            break;
                        }
                    }

                    if (connectionExecuted) {
                        break;
                    }
                }

                m_selectedPort = null;
                canvas.selectedPort = null;
                e.Use ();
            }
        }

    }


}
#endif